<template>
  <div>
    <el-header>
      <span>我的文件</span>
      <el-button @click="getFileInfo">刷新文件</el-button>
    </el-header>
    <el-table
      :data="tableData.filter(data => !search||data.timestamp.includes(search) ||data.originalname.toLowerCase().includes(search.toLowerCase()))"
      :default-sort="{prop:tableData.timestamp,order:'descending'}"
      style="width: 100%"
    >
      <el-table-column label="Date" prop="timestamp" sortable></el-table-column>
      <el-table-column label="FileName" prop="originalname" sortable></el-table-column>
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="deleteFile(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-upload
      class="upload-demo"
      ref="upload"
      action="http://localhost:3000/home/uploadfile"
      :headers="myHeaders"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :auto-upload="false"
    >
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
    </el-upload>
  </div>
</template>
<script>
const NodeRSA = require("node-rsa");
const crypto = require("crypto");
const BASE64 = "base64";
var token = localStorage.getItem("token");
export default {
  data() {
    return {
      tableData: [],
      search: "",
      deleteFileInfo: {
        fileid: "",
        random: "",
        signedData: ""
      },
      fileList:[],
      myHeaders: { Authorization: token }
    };
  },
  mounted: function() {
    this.getFileInfo();
  },
  methods: {
    submitUpload() {
      //此处必须打印一下token  否则取不到token
      this.$refs.upload.submit();
    },
    getFileInfo() {
      this.$axios.post("/home/getfileinfo").then(res => {
        // // console.log(res.data.content);
        if (res.data.type === "success") {
          // // console.log(res.data.content);
          this.tableData = res.data.content;
        } else {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
        }
      });
    },
    deleteFile(index, row) {
      this.deleteFileInfo.fileid = row.fileid;
      // 生成随机数并对随机数用私钥签名
      // 生成随机数并对随机数签名
      var buf = crypto.randomBytes(16);
      var random = buf.toString("hex");
      this.deleteFileInfo.random = random;

      var privateKey = new NodeRSA({ b: 512 });
      privateKey.importKey(window.localStorage.getItem("privateKey"));
      var signedData = privateKey
        .sign(Buffer.from(this.deleteFileInfo.random), BASE64)
        .toString(BASE64);
      this.deleteFileInfo.signedData = signedData;
      // // console.log(this.deleteFileInfo);

      // 发起删除文件的请求
      this.$axios.post("/home/deleteFile", this.deleteFileInfo).then(res => {
        if (res.data.type === "success") {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
          this.refresh();
        } else {
          this.$message({
            message: res.data.content,
            type: res.data.type
          });
        }
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    }
  }
};
</script>
<style scoped>
.el-header {
  background-color: #6d7981;
  display: flex;
  justify-content: space-between;
  color: #ffffff;
  font-size: 20px;
  align-items: center;
}
</style>